package com.corpgovernment.contract.controller;

import com.corpgovernment.common.base.BaseUserInfo;
import com.corpgovernment.common.base.JSONResult;
import com.corpgovernment.contract.service.ContractInfoService;
import com.corpgovernment.contract.vo.ContractInfoVo;
import com.ctrip.corp.obt.generic.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
@RequestMapping("/contractinfo")
public class ContractInfoController {

    @Autowired
    private ContractInfoService contractInfoService;
    /**
     * 保存合同（新增/修改）
     * @param request
     * @return
     */
    @RequestMapping("/save")
    public JSONResult save(@RequestBody ContractInfoVo request, BaseUserInfo baseUserInfo){
        request.setUid(baseUserInfo.getUid());
        request.setUname(baseUserInfo.getUserName());
        return new JSONResult(contractInfoService.save(request));
    }

    /**
     * 根据主键ID业务删除
     * @param request
     * @return
     */
    @RequestMapping("/delete")
    public JSONResult delete(@RequestBody ContractInfoVo request){
        return new JSONResult(contractInfoService.deleteById(request.getId()));
    }

    /**
     * 分页查询（服务商端和运营端共用根据标记区分）
     * @param request
     * @return
     */
    @RequestMapping("/selectContractInfoPage")
    public JSONResult selectContractInfoPage(@RequestBody ContractInfoVo request, BaseUserInfo baseUserInfo){
        if (StringUtils.isBlank(request.getSupplierCode()) && StringUtils.isNotBlank(baseUserInfo.getSupplierCode())){
            request.setSupplierCode(baseUserInfo.getSupplierCode());
        }
        return new JSONResult(contractInfoService.selectContractInfoPage(request));
    }

    /**
     * 修改合同状态（未生效/生效/失效）
     * @param request
     * @param baseUserInfo
     * @return
     */
    @RequestMapping("/validStatusUpdate")
    public JSONResult validStatusUpdate(@RequestBody ContractInfoVo request, BaseUserInfo baseUserInfo){
        return new JSONResult(contractInfoService.validStatusUpdate(request));
    }

    /**
     * 发送供应商
     * @param request
     * @param baseUserInfo
     * @return
     */
    @RequestMapping("/sendStatusUpdate")
    public JSONResult sendStatusUpdate(@RequestBody ContractInfoVo request, BaseUserInfo baseUserInfo){
        return new JSONResult(contractInfoService.sendStatusUpdate(request));
    }

    /**
     * 定时任务将合同日期超过今天的设置未失效
     * @return
     */
    @RequestMapping("/validStatusJob")
    public JSONResult<Boolean> validStatusJob(){
        return new JSONResult(contractInfoService.validStatusJob());
    }
}
